import { ts, SwitchStatement, rule, BreakStatement, is, CallExpression, BinaryExpression } from "qvog-dsl";

const SEVERITY = 2;
const DOC_PATH = 'docs/consistent-type-imports-check.md';
const DESCRIPTION = 'Enforce consistent usage of type imports.';

let disallowTypeAnnotations = true;

function getSpecifyChild(aNode: ts.Node, kind: ts.SyntaxKind): ts.Node | undefined {
    for (const child of aNode.getChildren()) {
        if (child.kind === kind) {
            return child;
        }
        let result = getSpecifyChild(child, kind);
        if (result) {
            return result;
        }
    }
    return undefined;
}

export default rule()
    .match((node): node is ts.TypeAliasDeclaration => ts.isTypeAliasDeclaration(node))
    .when((node): boolean => {
        const nodeList = node.getChildren();
        for (const child of nodeList) {
            let object = child;
            if (ts.isUnionTypeNode(child)) {
                const node = getSpecifyChild(child, ts.SyntaxKind.ImportType);
                if (node) {
                    object = node;
                }
            }
            if (ts.isImportTypeNode(object) && disallowTypeAnnotations) {
                return true;
            }
        }
        return false;
    })
    .report({
        severity: SEVERITY,
        description: DESCRIPTION,
        docPath: DOC_PATH,
    });
